package com.zhifa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhifa.Util.ComboboxUtil;
import com.zhifa.Util.ResponseUtil;
import com.zhifa.Util.StringUtil;
import com.zhifa.entity.Position;
import com.zhifa.service.PositionService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/position")
public class PositionController {

	@Resource
	PositionService positionService;
	
	@RequestMapping("/get")
	public String findPositionsByParentid(@RequestParam("parentid") Integer parentid,HttpServletResponse response) throws Exception{
		List<Position> positions = positionService.findPositionsByParentid(parentid);
		JSONArray comboboxJson = ComboboxUtil.positioncombobox(positions) ;
		ResponseUtil.write(response, comboboxJson);
		return null;
	}
	@RequestMapping("/getall")
	public String findPositions(HttpServletResponse response) throws Exception{
		List<Position> positions = positionService.findPositionsByParentid(Integer.parseInt("0"));
		JSONArray comboboxJson = ComboboxUtil.positioncombobox(positions) ;
		ResponseUtil.write(response, comboboxJson);
		return null;
	}
	
	@RequestMapping("/addnewaddposition")
	public String addnewaddposition(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String position1 = request.getParameter("position1");
		Map<String, Object>map=new HashMap<>();
		if (StringUtil.isEmpty(position1)) {
			map.put("parentid", Integer.parseInt("0"));
		}else {
			map.put("parentid", Integer.parseInt(position1));
		}
		String position2 = request.getParameter("position2");
		String standard = request.getParameter("sd");
		map.put("standard", standard);
		map.put("name", position2);
		int positioncount = positionService.addposition(map);
		
		JSONObject result=new JSONObject();
		if(positioncount>0){ 
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
}
